/*
 * Copyright 2011-2012 Redia A/S
 * 
 * This file is part of Indhold+ for Android.
 *
 *  Redia Library App for Android is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redia Library App for Android is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redia Library App for Android.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.digiform.model;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import dk.redia.lib.log.Log;
import dk.redia.lib.utils.AppApplication;
import dk.redia.lib.xmlrpc.XmlRpcClientResultListener;
import dk.redia.lib.xmlrpc.XmlRpcResultDumper;
import dk.redia.lib.xmlrpc.XmlRpcResultUtils;
import dk.redia.lib.xmlrpc.covers.CoversXmlRpcClient;

public class CoverUrlCache {
	
	private static CoverUrlCache instance;
	private static String logTag = "CoverUrlCache";
	
	private Set<CoverUrlCacheListener> listeners;
	private Map<String, String> cache;
	
	public static CoverUrlCache getInstance() {
		if (instance==null) {
			instance = new CoverUrlCache();
		}
		return instance;
	}
	
	private CoverUrlCache() {
		cache = new HashMap<String, String>();
		listeners = new HashSet<CoverUrlCache.CoverUrlCacheListener>();
	}
	
	public void addListener(CoverUrlCacheListener listener) {
		listeners.add(listener);
	}
	
	public void removeListener(CoverUrlCacheListener listener) {
		listeners.remove(listener);
	}
	
	public void fetch(String[] faustIds) {
		CoversXmlRpcClient.getInstance().getCoverUrlsFromArray(faustIds, new XmlRpcClientResultListener() {
			
			public void callFailed(String message) {
				Log.e(logTag, "Call to covers XML-RPC service failed: " + message);
			}
			
			public void callCompleted(Object resultObj) {
				Map<String, Object> resultMap = XmlRpcResultUtils.unpackStandardResult(resultObj);
				if (resultMap.containsKey("result")) {
					try {
						Boolean result = (Boolean)resultMap.get("result");
						if (!result) {
							Log.e(logTag, "Result was faulse in call to covers web service");
							return;
						}
					} catch (ClassCastException e) {
						Log.e(logTag, "Result was not a boolean in call to covers web service");
						return;
					}
				}
				if (resultMap.containsKey("data")) {
					boolean cacheChanged = false;
					Map<String, Object> dataMap = XmlRpcResultUtils.unpackStruct(resultMap.get("data"));
					for (String string : dataMap.keySet()) {
						try {
							String url = (String)dataMap.get(string);
							if (url!=null && !url.isEmpty()) {
								cache.put(string, url);
								cacheChanged = true;
							}
						} catch (ClassCastException e) {
							Log.e(logTag, "Invalid URL (not a string) in result from covers web service");
							continue;
						}
					}
					if (cacheChanged) {
						for (CoverUrlCacheListener listener : listeners) {
							listener.cacheUpdated();
						}
					}
				} else {
					Log.e(logTag, "No data entry in result from call to covers web service");
					return;
				}
				
			}
		});

	}
	
	public interface CoverUrlCacheListener {
		public void cacheUpdated();
	}

	public String getCoverUrl(String faustId) {
		if (cache.containsKey(faustId)) {
			return cache.get(faustId);
		} else {
			return null;
		}
	}
	
}
